package com.lanxiaoye.shoppingSystem.web;

import com.lanxiaoye.shoppingSystem.common.exception.BaseException;
import com.lanxiaoye.shoppingSystem.common.exception.EmptyException;
import com.lanxiaoye.shoppingSystem.common.page.HistoryPage;
import com.lanxiaoye.shoppingSystem.common.page.Page;
import com.lanxiaoye.shoppingSystem.common.result.IndexResult;
import com.lanxiaoye.shoppingSystem.entity.OwnEntity;
import com.lanxiaoye.shoppingSystem.service.HomeIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by libing on 2016/12/5.
 */
@Controller
@RequestMapping("/index")
public class HomeIndexController extends BaseController{

    @Autowired
    private HomeIndexService homeIndexService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request) throws Exception {
        return "login/login";
    }

    @RequestMapping("/getOwnEntity")
    @ResponseBody
    public IndexResult getOwnEntity(Model model, HttpServletRequest request, HttpSession session, Integer p){
        IndexResult indexResult = new IndexResult();
        List<OwnEntity> ownEntityHistoryList = new ArrayList<>();
        List<OwnEntity> ownEntityList = null;
        try {
            if (session.getAttribute("user_phone") == null){
                throw new EmptyException("[HomeIndexController][getOwnEntity] Index000:session user_phone is empty");
            }
            //如果搜索历史为空，则随便推荐商家,否则根据搜索历史推荐商家
            HistoryPage historyPage = HistoryPage.instance(1,6,session.getAttribute("user_phone").toString(), Integer.parseInt(session.getAttribute("user_type").toString()));
            List<String> historyList = homeIndexService.getMostLikeSearch(historyPage);
            //根据搜索历史去返回三个推荐商家
            if (historyList != null && historyList.size() > 0) {
                for (String content : historyList) {
                    System.out.println("======content:" + content);
                    OwnEntity ownEntity = homeIndexService.getOwnEntity(content);
                    if (ownEntity != null) {
                        ownEntityHistoryList.add(ownEntity);
                    }
                }
            }
            Page page = Page.instance(p,6);
            page.setTotalRows(homeIndexService.getOwnNumbers(page));
            ownEntityList = homeIndexService.getOwnEntityList(page);
            if ( ownEntityList.size() == 0) {
                throw new EmptyException("[HomeIndexController][getOwnEntity] Index001:ownEntityList is empty");
            } else {
                indexResult.setMsg("success");
                indexResult.setStatus("Index002");
            }
        } catch (EmptyException e) {
            indexResult.setMsg("Index_EmptyException");
            indexResult.setStatus(e.getMessage());
            loggerService.insertLog(e.getMessage());
        }catch (BaseException e){
            indexResult.setMsg("Index_BaseException");
            indexResult.setStatus(e.getMessage());
            loggerService.insertLog(e.getMessage());
        }
        indexResult.setOwnEntityHistoryList(ownEntityHistoryList);
        indexResult.setOwnEntityList(ownEntityList);
        return indexResult;
    }

}
